'use strict';
var path = require('path');
var require_transform = require('./transforms/require-transform.js');

function absolute(a){
    if (Array.isArray(a))
        return a.map(function(r){ return path.join(__dirname, r); });
    var ret = {};
    Object.keys(a).forEach(function(r){
        ret[path.join(__dirname, r)] = a[r]; });
    return ret;
}
module.exports = function(grunt) {
  let pkg = grunt.file.readJSON('package.json');
  let version = pkg.version;
    require('time-grunt')(grunt);
    grunt.initConfig({
        pkg: pkg,
        jshint: {
            options: {jshintrc: '.jshintrc'},
            all: ['src/*.js', './*.js'],
        },
clean: {
            dist: ['dist/*'],
            patched: ['src/yp-player.patched.js'],
        },
        replace: {
            dist: {
                src: ['src/yp-player.js'],
                dest: 'src/yp-player.patched.js',
                replacements: [{
                    from: 'require(\'@hola.org/hap.js/lib/hola_videojs_hls.js\')',
                    to: 'yp_vjs_provider_require()'
                }]
            }
        },
        browserify: {
            options: {
                browserifyOptions: {
                    standalone: 'yp-player',
                    debug: true,
                    noParse: absolute([
                        './node_modules/video.js/dist/video.js',
                    ]),
                },
                transform: [
                    ['browserify-versionify', {
                        placeholder: '__VERSION__',
                        version: pkg.version,
                    }],
                    'browserify-css',
                    'brfs',
                ],
                plugin: ['browserify-derequire'],
            },
            watch: {
                options: {
                    watch: true,
                    keepAlive: true,
                },
                files: {'dist/yp-player.dev.js': ['src/yp-player.js']},
            },
            vjs: {
                files: {'work/yp-vjs.dev.js':
                    ['./node_modules/@hola.org/hap.js/lib/hola_videojs_hls.js']
                },
            },
            dist: {
                files: {
                    'work/yp-player.dev.js': ['src/yp-player.patched.js'],
                },
                options: {
                    ignore: [
                        'videojs-contrib-dash',
                        './node_modules/@hola.org/dashjs/**'
                    ],
                },
            },
        },
        exorcise: {
            vjs: {
                files: {
                    'work/yp-vjs.dev.js.map': ['work/yp-vjs.dev.js'],
                },
            },
            dist: {
                files: {
                    'work/yp-player.dev.js.map': ['work/yp-player.dev.js'],
                },
            },
        },
less: {
      options: {
        paths: ['src/css/']
      },
      all: {
        files: [
          {
            nonull: true,
            dest: 'src/css/videojs-hola-skin.css',
            src: ['src/css/skin-*.less']
          },
{
            nonull: true,
            dest: 'src/css/videojs-settings.css',
            src: ['src/css/videojs-settings.less']
          }
        ]
      }
    },
        uglify : {
            options: {sourceMap: false},
            vjs: {
                files: {'work/yp-vjs.js': 'work/yp-vjs.dev.js'},
            },
            dist: {
                files: {'work/yp-player.js': 'work/yp-player.dev.js'},
            },
        },
version: {
      options: {
        prefix: '',
        replace: '\\{\\[version\\]\\}'
      },
      src: ['dist/js/videojs-hola-skin.js']
    },
        concat: {
            options: {
                banner: `(function(){
var yp_vjs_provider_require, hola_player_api;
var E = yp_player_api = {};
E.zdot = function(name){
    // zdot_stub:
    // zdot_stub:return {
    // zdot_stub:customer: {[=json it.customer]},
    // zdot_stub:json: {[=json it.json]},
    // zdot_stub:}[name];
    return {}[name];
};
E.customer = E.zdot('customer');
E.disable = function(){};
function yp_player_init(){
`, separator: `
};
yp_vjs_provider_require = function(){
var res, define = function(name, fn){res=fn();};
define.amd = true;
`, footer: `
return res;
};
yp_player_init();
})();`, sourceMap: true,
            },
            dist: {
                files: {'dist/yp-player.js':
                    ['work/yp-player.js', 'work/yp-vjs.js'],
                    'dist/yp-player.dev.js':
                    ['work/yp-player.dev.js', 'work/yp-vjs.dev.js'],
                }
            },
        },
        zip: {
            dist: {
                router: function(filepath){
                    return path.relative('dist', filepath);
                },
                src: ['dist/**/*'],
                dest: 'dist/yp-player-'+pkg.version+'.zip',
            },
        },
    });
    require('load-grunt-tasks')(grunt);
    grunt.loadNpmTasks('chg');
    grunt.registerTask('build', ['clean', 'jshint', 'less', 'version', 'replace',
        'browserify:vjs', 'browserify:dist', 
        'clean:patched', 'exorcise', 'uglify', 'concat', 'zip']);
    grunt.registerTask('default', ['build']);
};
